package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.response.OrderStatisticResp;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.ChargeService;
import com.ruoyi.business.service.ITOrderInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.GoogleGenerator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单列表Controller
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Controller
@RequestMapping("/business/orderInfo")
public class TOrderInfoController extends BaseController
{
    private String prefix = "business/orderInfo";

    @Autowired
    private ITOrderInfoService tOrderInfoService;

    @Resource
    private ChargeService chargeService;


    @RequiresPermissions("business:orderInfo:view")
    @GetMapping()
    public String orderInfo( ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser.getMerchantType().equals("0")){
            mmap.put("isShow",false);
        }else {
            mmap.put("isShow",true);
        }
        return prefix + "/orderInfo";
    }

    /**
     * 查询订单列表列表
     */
    @RequiresPermissions("business:orderInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TOrderInfo tOrderInfo)
    {
        startPage();
        SysUser sysUser = ShiroUtils.getSysUser();
        List<TOrderInfo> list = null;
        if(sysUser.getMerchantType().equals("0")){
           list = tOrderInfoService.selectTOrderInfoListBySh(tOrderInfo);
        }else{
            list = tOrderInfoService.selectTOrderInfoList(tOrderInfo);

        }

        return getDataTable(list);
    }

    /**
     * 导出订单列表列表
     */
    @RequiresPermissions("business:orderInfo:export")
    @Log(title = "订单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TOrderInfo tOrderInfo)
    {
        List<TOrderInfo> list = tOrderInfoService.selectTOrderInfoList(tOrderInfo);
        ExcelUtil<TOrderInfo> util = new ExcelUtil<TOrderInfo>(TOrderInfo.class);
        return util.exportExcel(list, "订单列表数据");
    }

    /**
     * 新增订单列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单列表
     */
    @RequiresPermissions("business:orderInfo:add")
    @Log(title = "订单列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TOrderInfo tOrderInfo)
    {
        return toAjax(tOrderInfoService.insertTOrderInfo(tOrderInfo));
    }

    /**
     * 修改订单列表
     */
    @RequiresPermissions("business:orderInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TOrderInfo tOrderInfo = tOrderInfoService.selectTOrderInfoById(id);
        mmap.put("tOrderInfo", tOrderInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单列表
     */
    @RequiresPermissions("business:orderInfo:edit")
    @Log(title = "订单列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TOrderInfo tOrderInfo)
    {
        return toAjax(tOrderInfoService.updateTOrderInfo(tOrderInfo));
    }

    /**
     * 删除订单列表
     */
    @RequiresPermissions("business:orderInfo:remove")
    @Log(title = "订单列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tOrderInfoService.deleteTOrderInfoByIds(ids));
    }

    @RequiresPermissions("business:orderInfo:reissue")
    @ResponseBody
    @PostMapping("/reissueOrder")
    @Log(title = "订单列表_强制补单", businessType = BusinessType.REISSUE)
    public AjaxResult forcePay(@RequestParam(value = "id") Long id, @RequestParam(value = "googleCode") String googleCode) throws Exception {
        GoogleGenerator googleGenerator = new GoogleGenerator();
        SysUser sysUser = ShiroUtils.getSysUser();
        boolean flag = googleGenerator.check_code(sysUser.getGoogleKey(), googleCode, System.currentTimeMillis());
        if (!flag) {
            return error("验证码错误");
        }
        long userId = ShiroUtils.getSysUser().getUserId();
        TOrderInfo orderInfoParam = tOrderInfoService.selectTOrderInfoById(id);
        orderInfoParam.setUpdateUser(userId);
        chargeService.forcePay(orderInfoParam);
        return toAjax(1);
    }

    @ResponseBody
    @PostMapping("/statistic")
    @Log(title = "订单统计", businessType = BusinessType.QUERY)
    public AjaxResult statistic(TOrderInfo orderInfoParam) {
        SysUser sysUser = ShiroUtils.getSysUser();
        OrderStatisticResp orderStatisticResp = null;
        if(sysUser.getMerchantType().equals("0")){
            orderStatisticResp = tOrderInfoService.statisticBySh(orderInfoParam);
        }else{
            orderStatisticResp = tOrderInfoService.statistic(orderInfoParam);

        }
        return AjaxResult.success(orderStatisticResp);
    }

    @ResponseBody
    @PostMapping("/smartReissueOrder")
    @Log(title = "智能补单", businessType = BusinessType.UPDATE)
    public AjaxResult smartReissueOrder(@RequestParam(value = "id") Long id) throws Exception {
        long userId = ShiroUtils.getSysUser().getUserId();
        TOrderInfo orderInfoParam = tOrderInfoService.selectTOrderInfoById(id);
        orderInfoParam.setUpdateUser(userId);
        AjaxResult responseData = chargeService.smartReissueOrder(orderInfoParam);
        return responseData;
    }

    //商户统计
    @RequiresPermissions("business:orderInfo:view")
    @GetMapping("/shStatisticsProfit")
    public String shStatisticsByChannelView(ModelMap mmap) {
        return prefix + "/shStatisticsProfit";
    }

    //商户统计
    @RequiresPermissions("business:orderInfo:view")
    @GetMapping("/msStatisticsProfit")
    @Log(title = "码商交易统计", businessType = BusinessType.QUERY)
    public String msStatisticsByChannelView(ModelMap mmap) {

        return prefix + "/msStatisticsProfit";
    }

    @ResponseBody
    @PostMapping("/shStatistic")
    @Log(title = "商户交易统计", businessType = BusinessType.QUERY)
    public TableDataInfo shStatistic(TOrderInfo orderInfoParam) {
        startPage();
        List<OrderStatisticResp> orderStatisticResps = tOrderInfoService.statisticByShList(orderInfoParam);
        return getDataTable(orderStatisticResps);
    }
    @ResponseBody
    @PostMapping("/msStatistic")
    @Log(title = "码商交易统计", businessType = BusinessType.QUERY)
    public TableDataInfo msStatistic(TOrderInfo orderInfoParam) {
        startPage();
        List<OrderStatisticResp> orderStatisticResps = tOrderInfoService.statisticList(orderInfoParam);
        return getDataTable(orderStatisticResps);
    }
}
